package comg.springboot.log.demo.interceptor;

import com.alibaba.fastjson.JSON;
import comg.springboot.log.demo.util.HttpContextUtil;
import comg.springboot.log.demo.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/19 14:38
 * @Version: 1.0
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        //从header中获取token
//        String token = request.getHeader("token");
//        //token为空
//        if(StringUtils.isBlank(token)){
//            throw new XjxccException("登录信息不存在，请重新登录", 601);
//        }
//
//        //查询token信息
//        TbUserToken tbToken = tokenService.queryByToken(token);
//        if(tbToken == null || tbToken.getExpireTime().getTime() < System.currentTimeMillis()){
//            throw new XjxccException("登录信息已失效，请重新登录", 602);
//        }
//
//        return true;

        log.info("---------------进入拦截器------------------请求地址{}",request.getRequestURI());

        //登录检查
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser == null){
//            throw new LoginException(601,"请重新登录");
            setReturn(response,601,"用户未登录，请先登录");
            return false;
        }
        log.info("用户信息："+loginUser);
        return true;
    }

    /**
     * 目标方法执行后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 页面渲染以后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    //返回错误信息
    private static void setReturn(HttpServletResponse response, int status, String msg) throws IOException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtil.getOrigin());
        //UTF-8编码
        httpResponse.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        Result build = Result.build(status, msg);
        String json = JSON.toJSONString(build);
        httpResponse.getWriter().print(json);
    }
}
